package demo;



/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-14
 * Time: 9:45
 */
public class TestDemo1 {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("helloworld");
        System.out.println(sb.delete(5, 10).insert(0, "你好"));
    }

    public static void main12(String[] args) {
        StringBuffer sb = new StringBuffer("abcdef");
        System.out.println(sb.reverse());
    }

    /**
     * StringBuffer 或  StringBuilder -> String
     * 调用 toString() 方法
     * @return
     */
    public static String func3(){
        StringBuffer sb =  new StringBuffer("abcde");
        return sb.toString();
    }

    /**
     * String -> StringBuffer 或  StringBuilder
     * 使用构造方法
     * @return
     */
    public static StringBuffer func1(){
        String str = "abcde";
        StringBuffer sb =  new StringBuffer(str);
        return sb;
    }
    /**
     * String -> StringBuffer 或  StringBuilder
     * 使用append()方法
     * @return
     */
    public static StringBuffer func2(){
        String str = "abcde";
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        return sb;
    }



    public static void main11(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("abced").append("1234");
        System.out.println(sb);
    }

    public static void main10(String[] args) {
        String str = "hll";
        str.intern();
        System.out.println(str);
    }

    public static String fistUpper(String s){
        if("".equals(s) || s==null){
            return s;
        }
        if(s.length()>1){
            return s.substring(0,1).toUpperCase()+s.substring(1);
        }
        return s.toUpperCase();
    }
    public static void main9(String[] args) {
        String str = "hello";
        String ret = fistUpper(str);
        System.out.println(ret);
    }

    public static void main8(String[] args) {
        String str1 = "";
        String str2 = "    ";//空格，长度不为0，所以该字符串不为空
        boolean flag1 = str1.isEmpty();
        boolean flag2 = str2.isEmpty();
        System.out.println(flag1);
        System.out.println(flag2);
    }

    public static void main7(String[] args) {
        String str = "hello";
        String ret = str.concat("world");
        System.out.println(ret);
    }

    public static void main6(String[] args) {
        String str = "abcdeFYKL";
        String ret = str.toLowerCase();
        System.out.println(ret);
    }

    public static void main5(String[] args) {
        String str = "   hello    world    " ;
        System.out.println("["+str+"]");
        System.out.println("["+str.trim()+"]");
    }

    public static void main4(String[] args) {
        String str = "abcdefg";
        //从下标为2的位置开始截取字符串
        String s = str.substring(2,4);
        System.out.println(s);
    }

    public static void main3(String[] args) {
        String str = "Java30 12&21#hello";
        String[] strings = str.split(" |&|#");
        for (String s:strings) {
            System.out.println(s);
        }
    }

    public static void main2(String[] args) {
        //使用"\\"表示'\'
        String str = "192\\168\\1\\1";
        //以 '\' 进行拆分，需要"\\\\"
        //因为'\'是一个特殊字符，要使用"\\"表示'\'，'\'又会对后面的字符进行转义，所以要使用4个
        String[] strings = str.split("\\\\");
        for (String s:strings) {
            System.out.println(s);
        }
    }

    public static void main1(String[] args) {
        String str = "hello world hello java";
        //将字符串以空格" " 部分拆分成两部分，不会均匀拆分
        String[] ret = str.split(" ",2);
        for (String s:ret) {
            System.out.println(s);
        }
    }
}
